﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using GameSchool.Models.Repositories;
using GameSchool.Models.Interfaces;
using GameSchool.Models;
using GameSchool.Models.ViewModels;
using System.IO;

namespace GameSchool.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/

        public ActionResult Index()
        {

            return View();
           
        }

        //Fetches a user profile
        public ActionResult Profile(Guid? ID)
        {
            UserRepository rep = new UserRepository();
            UserProfileModel model = new UserProfileModel();
            UserProfile u = new UserProfile();

            if (ID == null)
            {
                 u = rep.GetUserProfileByID((Guid)Membership.GetUser().ProviderUserKey);
            }
            else
            {
                 u = rep.GetUserProfileByID(ID);
            }

            model.Name = u.Name;
            model.Mobile = u.Mobile;
            model.SSN = u.SSN;
            model.userID = u.UserID;
            model.DateOfBirth = u.DateOfBirth;
            model.Address = u.Address;

            return View(model);

        }

        public ActionResult Achievement()
        {
            return View();
        }


        //Profile picture upload for User Profiles
       [HttpPost]
        public ActionResult Profile(UserProfileModel upload)
        {
            UserRepository userRep = new UserRepository();

           //Finding the userProfile we're editing.

           /*Now we need to save the picture uploaded by the user. For the sake of this assignment we'll just be saving it in our Content folder
            We have a specific path containing all user images - and then each user has his own folder were we store all the images he as uploaded.
            So we combine the userID into the pathname, and save the image there. */
           var path = Path.Combine(Server.MapPath("~/Content/img/userimage/"), Convert.ToString(upload.userID));
          
           /*Creating the userspecific directory in our content/img/userimage/ folder*/
           if(!Directory.Exists(path))
                Directory.CreateDirectory(path);

           /*We add the filename into the path, save the picture and then add the filepath into the DB*/
           path = Path.Combine(path, upload.Picture.FileName);
           upload.Picture.SaveAs(path);
           userRep.EditUserProfile(upload, path);
        
           return RedirectToAction("Index","Home");
        }
     
    }
}
